import React, { Component } from 'react'

export default class Count extends Component {
	state = {sum:0} //初始化状态
	render() {
		return (
			<div>
				<h3>当前求和为：{this.state.sum}</h3>
				<select ref={c => this.selectNumber = c}>
					<option value="1">1</option>
					<option value="2">2</option>
					<option value="3">3</option>
				</select> &nbsp;
				<button onClick={this.increment}>+</button>&nbsp;
				<button onClick={this.decrement}>-</button>&nbsp;
				<button onClick={this.incrementOdd}>奇数再+</button>&nbsp;
				<button onClick={this.incrementWait}>等一等再+</button>
			</div>
		)
	}
	//加法
	increment = ()=>{
		//获取原状态
		const {sum} = this.state
		//获取用户的输入
		const {value} = this.selectNumber
		//更新状态
		this.setState({sum:sum+value*1})
	}
	//减法
	decrement = ()=>{
		//获取原状态
		const {sum} = this.state
		//获取用户的输入
		const {value} = this.selectNumber
		//更新状态
		this.setState({sum:sum-value*1})
	}
	//加法（奇数）
	incrementOdd = ()=>{
		//获取原状态
		const {sum} = this.state
		if(sum % 2){
			//获取用户的输入
			const {value} = this.selectNumber
			//更新状态
			this.setState({sum:sum+value*1})
		}
	}
	//加法（等一等）
	incrementWait = ()=>{
		setTimeout(()=>{
			//获取原状态
			const {sum} = this.state
			//获取用户的输入
			const {value} = this.selectNumber
			//更新状态
			this.setState({sum:sum+value*1})
		},500)
	}
}
